package com.qf.mybatis03.service.impl;

import com.qf.mybatis03.dao.BookDao;
import com.qf.mybatis03.entity.Book;
import com.qf.mybatis03.service.BookService;
import com.qf.mybatis03.utils.MyCache;
import com.qf.mybatis03.utils.MybatisSqlSessionUtils;

import javax.jnlp.BasicService;

public class BookServiceImpl implements BookService {
    public static String prfix = "book";

    @Override
    public Book getBook(Integer id) {
        String key = prfix + ":" + id;
        Book book = (Book) MyCache.cache.get(key);
        if (book != null) {
            return book;
        }
        BookDao bookDao = MybatisSqlSessionUtils.getMapper(BookDao.class);
        Book dbBook = bookDao.getBookById(id);
        //3.更新缓存中的数据
        MyCache.cache.put(key, dbBook);
        return dbBook;
    }
}
